﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using eeenno.ReaderEngine.Objects;

namespace eeenno.ReaderEngine.Helpers
{
    /// <summary>
    /// Subscription utility class
    /// </summary>
    public class SubscriptionHelper
    {
        /// <summary>
        /// Builds a subscriptions list using the unread count and subscriptions data
        /// </summary>
        /// <param name="unread_counts">unread counts data</param>
        /// <param name="subscriptions">list of subscriptions</param>
        /// <returns>subscriptions list</returns>
        public static List<Subscription> BuildSubscriptionList(string[] unread_counts, string[] subscriptions)
        {
            List<Subscription> ret_val = new List<Subscription>(subscriptions.Length);

            for (int i = 0; i < ret_val.Capacity; i++)
            {
                string[] current_sub_data = subscriptions[i].Split(',');
                Subscription current = new Subscription();
                current.url = current_sub_data[0];
                current.title = current_sub_data[1];
                current.tag = current_sub_data[2];
                //retrieve the unread count by looping through each value
                for (int j = 0; j < unread_counts.Length; j++)
                {
                    string[] current_unread_data = unread_counts[j].Split(',');
                    current.unread_count = 0;
                    if (current.url.Equals(current_unread_data[0]))
                    {
                        current.unread_count = int.Parse(current_unread_data[1]);
                        break;
                    }
                }
                ret_val.Add(current);
            }

            return ret_val;
        }


    }
}
